const sha1=require('sha1');
const service={
    RandomInt:async ()=>{
        let result='';
        for(let i=0;i<6;i++){
            result+=parseInt(Math.random()*10).toString();
        }
        return result;
    },
    createUUID:()=>{
        let s = [];
        const hexDigits = "0123456789abcdef";
        for (var i = 0; i < 32; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4";
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);

        let uuid = s.join("");
        return uuid;
    },
    createHeader:async ()=>{
        let CurTime = Date.parse(new Date())/1000;
        let AppSecret='313b3101bc8e';
        let AppKey= '89be64f1cd0aab5972dd050480a42e05';
        let Nonce=await service.RandomInt();
        Nonce=Nonce.toString();
        CurTime=CurTime.toString();
        let CheckSum=sha1(AppSecret+Nonce+CurTime);
        let headers={
            'Content-Type':'application/x-www-form-urlencoded;charset=utf-8',
            'AppKey':AppKey,
            'Nonce':Nonce,
            'CurTime':CurTime,
            'CheckSum':CheckSum
        };
        return headers
    }
};

module.exports=service;